//***************************************************************************************
// MediaFrame is an Open Source streaming media platform in Java 
// which provides a fast, easy to implement and extremely small applet 
// that enables to view your audio/video content without having 
// to rely on external player applications or bulky plug-ins.
//
//--------------------------------------------------------------------------------------
//
// We changed a lot of code and added a lot of functionality.
// This includes, but not limited to, the following changes:
// 1. The project was renamed to MediaFrame;
// 2. The connection speed detection procedure was added;
// 3. The JavaScript API functions were added;
// 4. The pre and post image support functionality was added;
// 5. The ability to save movie into the local disk was added;
// 6. The inner buffer for a movie file was added;
// 7. The click-through functionality was added;    
// 8. The .zip files support was added;    
// 9. The realtime feedback agent functionality was added.    
// For the full list of the current functionality please visit the following web page:
// http://mediaframe.org/
//    
// 06 Jul 2002 - 19 Dec 2004 Konstantin Belous, Oleg Lebedev
//
//--------------------------------------------------------------------------------------
//
// This program is free software; you can redistribute it and/or modify it under the
// terms of the GNU General Public License as published by the Free Software
// Foundation; either version 2 of the License, or (at your option) any later version.
//
// This program is distributed in the hope that it will be useful, but WITHOUT ANY
// WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS FOR A
// PARTICULAR PURPOSE. See the GNU General Public License for more details.
//
// You should have received a copy of the GNU General Public License along with this
// program; (See "LICENSE.GPL"). If not, write to the Free Software Foundation, Inc.,
// 59 Temple Place - Suite 330, Boston, MA  02111-1307, USA.
//
//--------------------------------------------------------------------------------------
//
// If the program runs as Java applet it isn't "interactive" in the sense of the GNU
// General Public License. So paragraph 2c doesn't apply.
//
//**************************************************************************************
package mediaframe.mpeg1.controlpanel;

import mediaframe.mpeg1.MPEG1;
import java.awt.*;
import java.awt.image.*;
import java.awt.event.MouseEvent;
import java.awt.event.MouseListener;
import java.awt.event.WindowAdapter;
import java.awt.event.WindowEvent;
import java.awt.event.AdjustmentListener;
import java.awt.event.AdjustmentEvent;


/**
 * The <code>ControlPanel</code> responds for painting of the control panel 
 * (play/pause, mute on/mute off, stop buttons).
 *
 * @author  Konstantin Belous
 * @version 1.0 26.07.2002
 *
 * @see IControlPanel
 *
 */
public class ControlPanel implements MouseListener, IControlPanel {
	
	/** The reference to the video player applet. */
	private MPEG1 player;
	/** The reference to the video screen. */
	private Panel screen;
	/** The play button of the control panel. */
	private ControlButton play;
	/** The stop button of the control panel. */
	private ControlButton stop;
	/** The mute button of the control panel. */
	private ControlButton speaker;
	
	/** The "played" video player state. */
	private static final int PLAYED   = 1;
	/** The "paused" video player state. */
	private static final int PAUSED   = 2;
	/** The "stopped" video player state. */
	private static final int STOPPED  = 3;
	/** The "disabled" video player state. */
	private static final int DISABLED = 4;

	/** The state of the video player. */
	private int state = DISABLED;

	/** The mute flag. */
	boolean mute = false;
	
	/**
	 * Initialization of the control panel. 
	 * Constructs the buttons (play/pause, mute on/mute off, stop).
	 * Disables the control panel after initialization.
	 *
	 * @param player the reference to the called applet.
	 * @param heigth height of the control panel.
	 *
	 */
	public void initPanel(MPEG1 player, int heigth){

		int h = heigth - 25;

		this.player = player;

		String [] ImagesFileNames = new String[2];

		ImagesFileNames[0] = "play_up.gif";
		ImagesFileNames[1] = "play_down.gif";

		play = new ControlButton(player, ImagesFileNames);

		play.loadHidden("pause.gif");

		ImagesFileNames[0] = "stop_up.gif";
		ImagesFileNames[1] = "stop_down.gif";

		stop = new ControlButton(player, ImagesFileNames);

		ImagesFileNames[0] = "mute_up.gif";
		ImagesFileNames[1] = "mute_down.gif";

		speaker = new ControlButton(player, ImagesFileNames);

		speaker.setBounds(10,h,18,17);
		play.setBounds(10+17,h,18,17);
		stop.setBounds(10+34,h,18,17);

		speaker.setBounds(10, h);
		play.setBounds(10 + 17, h);
		stop.setBounds(10 + 34, h);

		
		screen = player.movieScreen;

		disablePanel();

		screen.add(play);
		screen.add(stop);
		screen.add(speaker);

		play.addMouseListener(this);
		stop.addMouseListener(this);
		speaker.addMouseListener(this);
		
		if(player.isSoundEnabled() == false){
			speaker.setEnabled(false);
		}
	}
	
	/**
	 * Paints the control panel. 
	 *
	 * @param g the graphics context of the video player.
	 *
	 */
	public void paint(Graphics g){
		if(play == null || stop == null || speaker == null)
			return;
		play.paint(g);
		stop.paint(g);
		speaker.paint(g);
	}

	/**
	 * The mouse is pressed on the button (Mouse Listener event).
	 * The method calls the player's logic to perform required action.
	 * 
	 * @param e the event state information.
	 * 
	 */
	public void mousePressed(MouseEvent e){

//		if(e.getModifiers() != java.awt.event.InputEvent.BUTTON1_MASK)
//			return;

		if(e.getSource() == speaker && speaker.isEnabled()){
			if(player.isSoundEnabled())
				player.playersetmute(!mute);
		} else if(e.getSource() == play && play.isEnabled()) {
			if(state == PLAYED)
				player.playerpause();
			else
				player.playerplay();
		} else if(e.getSource() == stop && stop.isEnabled()) {
			player.playerrewind();
		}
	}
	
	/**
	 * Stub for the MouseListener interface (does nothing).
	 * 
	 */
	public void mouseReleased(MouseEvent e){}
	
	/**
	 * Stub for the MouseListener interface (does nothing).
	 * 
	 */
	public void mouseEntered(MouseEvent e){
		player.mouseEntered(e);
	}
	
	/**
	 * Stub for the MouseListener interface (does nothing).
	 * 
	 */
	public void mouseExited(MouseEvent e){}
	
	/**
	 * Stub for the MouseListener interface (does nothing).
	 * 
	 */
	public void mouseClicked(MouseEvent e){
	}

	/**
	 * Sets the mute value. 
	 *
	 * @param mute the mute value.
	 *
	 */
	public void setMute(boolean mute){

		if (this.mute == mute)
			return;

		this.mute = mute;

		if(mute) {
			speaker.setVolumeDisabled();
		} else {
			speaker.setVolumeEnabled();
		}
		screen.repaint();
	}
	
	/**
	 * Gets mute value. 
	 *
	 * @return the mute value.
	 *
	 */
	public boolean getMute() {
		return mute;
	}
	
	/**
	 * Implements "play" action of the control panel. 
	 *
	 */
	public void play(){		if(state == PLAYED)
			return;
		state = PLAYED;
		play.rotateHidden();
		play.setEnabled(true);
		stop.setEnabled(true);
	}
	
	/**
	 * Implements "stop" action of the control panel. 
	 *
	 */
	public void stop(){
		if(state == PLAYED)
			play.rotateHidden();
		state = STOPPED;
		play.setEnabled(true);
		stop.setEnabled(false);
		screen.repaint();
	}
	
	/**
	 * Implements "end play" action of the control panel. 
	 *
	 */
	public void endPlay(){
//		if(state == DISABLED)
//			return;
		if(state == PLAYED)
			play.rotateHidden();
		state = STOPPED;
		play.setEnabled(false);
		stop.setEnabled(true);
		screen.repaint();
	}
	
	/**
	 * Implements "pause" action of the control panel. 
	 *
	 */
	public void pause(){
		if(state != PLAYED)
			return;
		state = PAUSED;
		play.rotateHidden();
		play.setEnabled(true);
		stop.setEnabled(true);
		screen.repaint();
	}
	
	/**
	 * Implements "disable" action of the control panel. 
	 *
	 */
	public void disablePanel(){
		if(state == PLAYED)
			play.rotateHidden();
		state = DISABLED;
		play.setEnabled(false);
		stop.setEnabled(false);
		screen.repaint();
	}
}
